<?php defined('SYSPATH') OR die('No direct access allowed.');


class myform_Core {
	
	

	
	public static function inputfield($field, $title, $content, $class="text", $id="" ){
	
		$r='';
		$r.='<p>';
		$r.='<div class="leftrow">';
		$r.=form::label($field , $title);
		$r.='</div>';
		$r.='<div class="rightrow">';
		$r.=form::input($field , $content, 'class="'.$class.'"  id="'.$id.'"');
		$r.='</div>';	
		$r.='</p>';
		
		return $r;
	}
	
	public static function password($field, $title, $content, $class="text", $id="" ){
	
		$r='';
		$r.='<p>';
		$r.='<div class="leftrow">';
		$r.=form::label($field , $title);
		$r.='</div>';
		$r.='<div class="rightrow">';
		$r.=form::password($field , $content, 'class="'.$class.'"  id="'.$id.'"');
		$r.='</div>';	
		$r.='</p>';
		
		return $r;
	}
	
	public static function textarea($field, $title, $content, $class="text" ){
		
		$r='';
		$r.='<p>';
		$r.='<div class="leftrow">';
		$r.=form::label($field , $title);
		$r.='</div>';
		$r.='<div class="rightrow">';
		$r.=form::textarea($field , $content, 'class="'.$class.'" ');
		$r.='</div>';		
		$r.='</p>';
		return $r;
	}
	
	public static function imgelm($title, $mediaid, $class="text" ){
	
		$r='';
		$r.='<p>';
		$r.='<div class="leftrow">';
		$r.=form::label($title , $title);
		$r.='</div>';
		$r.='<div class="rightrow">';
		$media=ORM::factory('media', $mediaid);
		$url=$media->get_resized_url(300,300);
		$r.=html::image($url);
		$r.='</div>';	
		$r.='</p>';
		
		return $r;
	}
	
	public static function textelm($title, $content, $class="text" ){
	
		$r='';
		$r.='<p>';
		$r.='<div class="leftrow">';
		$r.=form::label($title , $title);
		$r.='</div>';
		$r.='<div class="rightrow">';
		$r.=nl2br(text::auto_link($content));
		$r.='</div>';	
		$r.='</p>';
		
		return $r;
	}
	
	public static function dropdown($field, $title, $content, $selected=NULL, $class="text" ){
	
		$r='';
		$r.='<p>';
		$r.='<div class="leftrow">';
		$r.=form::label($field , $title);
		$r.='</div>';
		$r.='<div class="rightrow">';
		$r.=form::dropdown($field , $content, $selected, 'class="'.$class.'" ');
		$r.='</div>';	
		$r.='</p>';
		
		return $r;
	}
	
	public static function checkbox($field, $title, $content, $class="checkbox" ){
	
		$r='';
		$r.='<p>';
		$r.='<div class="leftrow">';
		$r.=form::label($field , $title);
		$r.='</div>';
		
		$r.='<div class="rightrow" >';
		$r.=form::hidden($field, 0);
		$r.= form::checkbox($field,1,$content, 'class="'.$class.'"');
		$r.='</div>';	
		$r.='</p>';
		return $r;
	}
	
	public static function checkbox2($field,$value, $title, $content, $class="checkbox" ){
	
		$r='';
		$r.='<p>';
		$r.='<div class="leftrow">';
		$r.=form::label($field , $title);
		$r.='</div>';
		$r.='<div class="rightrow" >';
		$r.= form::checkbox($field, $value , $content, 'class="'.$class.'"');
		$r.='</div>';	
		$r.='</p>';
		return $r;
	}
	
	public static function upload($field, $title, $class="" ){
		$r='';
		$r.='<p>';
		$r.='<div class="leftrow">';
		$r.=form::label($field , $title);
		$r.='</div>';
		$r.='<div class="rightrow">';
		$r.= form::upload(array('name' => $field, 'class'=>$class));
		$r.='</div>';	
		$r.='</p>';
		
		return $r;
	}
	
	public static function fieldset($title=""){
		$r='';
		$r.= form::close_fieldset();
		$r.= form::legend($title);
		$r.= form::open_fieldset();		
		return $r;
	}
	
	public static function statusselector($content){
		
		$options=array('draft'=>'draft', 'published'=>'published', 'secured'=>'secured');
		$title='Status';
		$field='status';
		$class='';

		$r='';
		$r.=myform::dropdown($field, $title,$options, $content,$class);
		
		return $r;
	}
	
	public static function submitbutton($field, $title){
	
		$r='';
		$r.='<p>';
		$r.='<div class="leftrow">';
		$r.='&nbsp;';
		$r.='</div>';
		$r.='<div class="rightrow">';
		$r.= form::submit($field, $title, 'class="submit_button"');
		$r.='</div>';	
		$r.='</p>';
		
		return $r;
		
	}
	
	public static function submit(){
	
		$r='';
		$r.= form::open_fieldset(array('class'=>'submit'));
		$r.='<p>';
		$r.='<div class="leftrow">';
		$r.='&nbsp;';
		$r.='</div>';
		$r.='<div class="rightrow">';
		$r.= form::submit('save', 'Save', 'class="submit_button"');
		$r.= form::submit('saveandclose', 'Save and Close', 'class="submit_button"');
		$r.= form::button('close', 'Close', 'class="close_button"');
		$r.='</div>';	
		$r.='</p>';
		
		$r.= form::close_fieldset();
		
		return $r;
	}
	
	public static function submit_small(){
	
		$r='';
		$r.= form::open_fieldset(array('class'=>'submit'));
		$r.='<p>';
		$r.='<div class="leftrow">';
		$r.='&nbsp;';
		$r.='</div>';
		$r.='<div class="rightrow">';
		$r.= form::submit('saveandclose', 'Save and Close', 'class="submit_button"');
		$r.= form::button('close', 'Close', 'class="close_button"');
		$r.='</div>';	
		$r.='</p>';
		
		$r.= form::close_fieldset();
		
		return $r;
	}
	
	public static function submit_close(){
	
		$r='';
		$r.= form::open_fieldset(array('class'=>'submit'));
		$r.='<p>';
		$r.='<div class="leftrow">';
		$r.='&nbsp;';
		$r.='</div>';
		$r.='<div class="rightrow">';
		$r.= form::button('close', 'Close', 'class="close_button"');
		$r.='</div>';	
		$r.='</p>';
		
		$r.= form::close_fieldset();
		
		return $r;
	}
	
	
	
	public static function media_selector($field, $title, $content){
		$categories=ORM::factory('category');
		$r='';
		$r.='<script type="text/javascript">	
		$(document).ready(function() {
			$("#category").change(function() {
					var catid=$(this).attr("value");
					var url="'.url::base(true).'ajaxview";
					$("#'.$field.' .catcont").html("loading images...");
					$.post(url,{catid:catid, field:"'.$field.'",viewfile:"media/media_selector_single"},	  
				 	function(data){
						$("#'.$field.' .catcont").html(data);
						
				  	});	
				});		
		});
		</script>
		';
		$r.='<p>';
		$r.='<div class="leftrow">';
		$r.=form::label($field , $title);
		$r.='</div>';
		
		$selectedcat=NULL;
		$selectedid=NULL;
		
		if($content!=''){	
			$img=ORM::factory('media', $content);
			$selectedcat=$img->category_id;
			$selectedid=$img->id;
		}
		
		$r.='<div class="rightrow">';
			$r.= '<div id="'.$field.'"  class="media_container">';				
				$r.= '<div class="navi">';	
					$r.= '<div class="mininavi">Kein Bild '.form::radio($field,'0').'</div>';	
					$cats=$categories->get_pulldown_list(array('0'=>'Nichts ausgewählt'));
					$r.='<div>'.form::dropdown('category',$cats, $selectedcat).'</div>';				
				
				$r.= '</div>';
				$r.= '<div class="catcont">';
				
				if($content){
					$subview=new View('media/media_selector_single');
					$subview->catid=$selectedcat;
					$subview->selectedid=$selectedid;
					$subview->field=$field;
					$r.= $subview;
					
				}
				
				$r.= '</div>';			
			$r.='</div>';
		$r.='</div>';	
		$r.='</p>';
		
		return $r;
	}
	
	public static function media_selector_multiple($field, $title){
		$categories=ORM::factory('category');
		$r='';
		$r.='<script type="text/javascript">	
		$(document).ready(function() {		
			$("#category").change(function() {
					var catid=$(this).attr("value");
					var url="'.url::base(true).'ajaxview";
					$("#'.$field.' .catcont").html("loading images...");
					$.post(url,{catid:catid, field:"'.$field.'",viewfile:"media/media_selector_multiple"},	  
				 	function(data){
						$("#'.$field.' .catcont").html(data);
				  	});	
				});		
			
			$(".selectall").click(function(e) {
					$("#'.$field.'").find("input:checkbox").attr("checked", true);
				});
			
			$(".deselectall").click(function(e) {
					$("#'.$field.'").find("input:checkbox").attr("checked", false);
				});
		});
		</script>
		';
		$r.='<p>';
		$r.='<div class="leftrow">';
		$r.=form::label($field , $title);
		$r.='</div>';
		
		
		$r.='<div class="rightrow">';
			$r.= '<div id="'.$field.'"  class="media_container">';				
				$r.= '<div class="navi">';	
					$r.= '<div class="mininavi"><a href="#" class="selectall" >all</a> | <a href="#"  class="deselectall" >none</a></div>';	
					$cats=$categories->get_pulldown_list(array('0'=>'Nichts ausgewählt'));
					$r.='<div>'.form::dropdown('category',$cats).'</div>';				
				$r.= '</div>';
				$r.= '<div class="catcont">';
				$r.= '</div>';
				
			$r.='</div>';
		$r.='</div>';	
		$r.='</p>';
		
		return $r;
	}
}